package com.joeyzh.utils;


import com.joey.ui.CheckedModel;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Joey
 * @author Joey
 * 自定义的选择布局搜索控件
 */
public class CheckedModelFilter<T extends CheckedModel> extends ModelFilter<T> {

    private List<T> defaults;

    public CheckedModelFilter(List<T> original, OnFilterListener filterListener) {
        super(original, filterListener);
        addMatcher("Name");
        addMatcher("Id");
    }

    public CheckedModelFilter addDefaultList(List<T> models) {
        defaults = models;
        return this;
    }

    @Override
    public boolean equals(Pattern pattern, T item) {
        boolean flag = super.equals(pattern, item);
        if (defaults == null || defaults.isEmpty()) {
            return flag;
        }
        for (CheckedModel model : defaults) {
            if (model.getId().equalsIgnoreCase(item.getId())) {
                return false;
            }
            if (model.getName().equals(item.getName())) {
                return false;
            }
        }
        return flag;
    }

    @Override
    protected List<T> doInBackground(String... params) {
        List<T> results = super.doInBackground(params);
        if (defaults != null) {
            results.addAll(0, defaults);
        }
        return results;
    }


}

